package com.santiao.youxiang.ui.adapter

import android.view.View
import com.allen.library.shape.ShapeLinearLayout
import com.santiao.youxiang.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.chauthai.swipereveallayout.SwipeRevealLayout
import com.santiao.youxiang.bean.habitBillBean

//import com.santiao.youxiang.database.Bill

//import com.santiao.youxiang.database.yibaoBill

class HabitAdapter(data: MutableList<habitBillBean>? = null/*, type: Int*/) :
    BaseQuickAdapter<habitBillBean, BaseViewHolder>(R.layout.item_habit_display, data) {
//         var types =type
// 添加这个属性来解决 "Unresolved reference" 错误
var onItemClickListener: OnItemClickListener? = null
    init {
        addChildClickViewIds(R.id.ll_edit) // 关键：声明子视图 ID
        addChildClickViewIds(R.id.ll_del) // 关键：声明子视图 ID
        addChildClickViewIds(R.id.ll_main) // 关键：声明子视图 ID
    }

    override fun convert(holder: BaseViewHolder, item: habitBillBean) {
        // 绑定数据到视图
//        Log.e( "convert: ",types.toString())



        holder.setText(R.id.tv_name,item.habitName)
        holder.setText(R.id.tv_day,item.habithold_day)
        holder.setText(R.id.tv_times,item.checkIns)
        holder.setText(R.id.tv_per_day,"/${item.habitToday_target}次")

//        val main = holder.getView<LinearLayout>(R.id.ll_main)
//
//        main.setOnClickListener {
//            onItemClickListener?.onItemClick(item )
//        }



//        val document = Jsoup.parse(item.content)
//        val textContent = document.text()
//        holder.setText(R.id.email_content,textContent)
////        holder.eContent.text=textContent
//        holder.itemView.setOnClickListener{
////            LZYLog.i("lzyemailget","${item.uid}")
//            val bundle=Bundle()
////            bundle.putInt("type", types)
//            bundle.putSerializable("email", item)
////            LZYBaseActivity.forward<EmailDetailActivity>(mContext,bundle)
//
//        }


        val swipeLayout = holder.getView<SwipeRevealLayout>(R.id.swipe_layout)
//        swipeLayout.setLockDrag(true) // 锁定滑动，但不影响点击
//        swipeLayout.enable = false // 关闭背景滑动
        val deleteView  = holder.getView<ShapeLinearLayout>(R.id.ll_del)
        val editView  = holder.getView<ShapeLinearLayout>(R.id.ll_del)
//
//        val linelayout  = holder.getView<LinearLayout>(R.id.ll_right)

        // 1) 初始隐藏
//        deleteView.visibility = View.INVISIBLE



        // 3) 监听滑动状态
       /* swipeLayout.setSwipeListener(object : SwipeRevealLayout.SwipeListener {
            override fun onClosed(view: SwipeRevealLayout?) {
                deleteView.isEnabled = false
                editView.isEnabled = false
//                linelayout.isEnabled = false
            }
            override fun onOpened(view: SwipeRevealLayout?) {
//                deleteView.visibility = View.VISIBLE
                deleteView.isEnabled = true
//                editView.visibility = View.VISIBLE
                editView.isEnabled = true

                linelayout.visibility = View.VISIBLE
//                linelayout.isEnabled = true
            }
            override fun onSlide(view: SwipeRevealLayout?, slideOffset: Float) {
                // 如果想滑动过程中就显示，可以在这里：
//                 deleteView.visibility = View.GONE
//                deleteView.visibility = if (slideOffset > 0f) View.VISIBLE else View.INVISIBLE

                linelayout.visibility = if (slideOffset > 0f) View.VISIBLE else View.INVISIBLE

            }
        })*/

        swipeLayout.setSwipeListener(object : SwipeRevealLayout.SwipeListener {
            override fun onClosed(view: SwipeRevealLayout?) {
                // 禁用交互，并隐藏视图
                deleteView.visibility = View.INVISIBLE
                editView.visibility = View.INVISIBLE

                deleteView.isEnabled = false
                editView.isEnabled = false
            }

            override fun onOpened(view: SwipeRevealLayout?) {
                // 启用交互，并显示视图
                deleteView.visibility = View.VISIBLE
                editView.visibility = View.VISIBLE

                deleteView.isEnabled = true
                editView.isEnabled = true
            }

            override fun onSlide(view: SwipeRevealLayout?, slideOffset: Float) {
                // 根据滑动偏移调整视图的可见性
                // 如果你想根据滑动偏移调整 `deleteView` 和 `editView` 显示的渐变效果，可以在这里做
                deleteView.visibility = if (slideOffset > 0f) View.VISIBLE else View.INVISIBLE
                editView.visibility = if (slideOffset > 0f) View.VISIBLE else View.INVISIBLE

                // 也可以控制它们的交互能力
                // 例如：如果你希望滑动时禁用 `deleteView` 和 `editView` 的交互：
                deleteView.isEnabled = slideOffset > 0f
                editView.isEnabled = slideOffset > 0f
            }
        })


        when (item.imageType){
    "1"->{
        holder.setImageResource(R.id.iv_photo,R.drawable.ic_habit_bg5)
    }

    "2"->{
        holder.setImageResource(R.id.iv_photo,R.drawable.ic_habit_bg6)
    }

    "3"->{
        holder.setImageResource(R.id.iv_photo,R.drawable.ic_habit_bg7)
    }

    "4"->{
        holder.setImageResource(R.id.iv_photo,R.drawable.ic_habit_bg8)
    }
    else -> {
        holder.setImageResource(R.id.iv_photo,R.drawable.ic_habit_bg9)
    }

}


//        holder.setText(R.id.tv_title, item.nickName)
//
//        Log.e( "convert: ","2" + item.nickName)
        // 初始化内层 RecyclerView
//        val rvMater: RecyclerView = holder.getView(R.id.rv_mater)
//        rvMater.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

//        // 设置内层适配器
//        val materialAdapter = DetailIngredientsAdapter(item.ingredients2 as MutableList<FoodSearchResbean.Ingredients>)
//        rvMater.adapter = materialAdapter
//
//        // 可选：禁用嵌套滚动（解决滑动冲突）
//        rvMater.isNestedScrollingEnabled = false

//        holder.setText(R.id.tv_num, item.portionSize)

//        var image=holder.getView<ImageView>(R.id.iv_photo)

//        GlideApp.with(this)        .load(R.raw.large_giphy_logo)        .into(giphyLogoView);

//        Glide.with(holder.itemView.context).load(item.imageUrl).into(image)
//        holder.setText(R.id.tv_content, item.content)

//        val radius = 15 // 半径为 20dp
//
//// 将 dp 转换为像素
//        val px = TypedValue.applyDimension(
//            TypedValue.COMPLEX_UNIT_DIP,
//            radius.toFloat(),
//            holder.itemView.context.resources.displayMetrics
//        ).toInt()
//
//        var image=holder.getView<ImageView>(R.id.iv_photo)
//
//        Glide.with(holder.itemView.context).load(item.banner)  .apply(RequestOptions.bitmapTransform(RoundedCorners(px))) .into(image)

    }

    interface OnItemClickListener {
        fun onItemClick(data: habitBillBean)
    }

}

